<?php

namespace app\model;

use think\Model;

class CustomerGenjin extends Model
{

    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'customer_genjin';

    public static function getnewone($cid)
    {
        $ret = self::where(['cid' => $cid])->find();
        if (!empty($ret)) {
            $ret = $ret->toArray();
        }
        return $ret;
    }

    public static function getlist($cid)
    {

        $list = self::where(['cid' => $cid])->order('id desc')->select()->toArray();

        if (!empty($list)) {
            foreach ($list as &$vo) {
                $vo['uname'] = Users::getusername($vo['uid']);
            }
        }

        return $list;
    }

    public static function toReceive($params)
    {

        $genjin['uid'] = $params['uid'];
        $genjin['cid'] = $params['cid'];
        $genjin['create_time'] = time();
        $genjin['update_time'] = time();
        $genjin['status'] = 1;
        $genjin['fuze'] = 0;

        if (Lingqunum::checknum($params['uid'])) {
            $Customerdata = Customer::where(['id' => $params['cid']])->find();
            if (empty($Customerdata->genjin)) {
                $cdata['id'] = $params['cid'];
                $cdata['genjin'] = $params['uid'];
                $cdata['gj_time'] = time();
                $genjin['fuze'] = 1;

                Customer::update($cdata);
                CustomerGenjin::create($genjin);
                Lingqunum::upnum($params['uid']);
                return 1;
            } else {
                return 2;
            }
        } else {
            return 0;
        }
    }
    public static function setGenjin($params)
    {

        $genjin['uid'] = $params['uid'];
        $genjin['cid'] = $params['cid'];

        $genjin['create_time'] = time();
        $genjin['update_time'] = time();
        $genjin['status'] = 1;
        $genjin['fuze'] = 0;

        if (!empty($params['head'])) {
            $cdata['id'] = $params['cid'];
            $cdata['genjin'] = $params['uid'];
            $cdata['gj_time'] = time();
            $genjin['fuze'] = 1;

            Customer::update($cdata);
        }
        CustomerGenjin::create($genjin);
        return 1;
    }
}
